#import <Foundation/Foundation.h>
#import <QuartzCore/QuartzCore.h>
#import <objc/runtime.h>

#import "FWStrikeoutView.h"
#import "FWCommon.h"


@implementation FWStrikeoutView

@synthesize strikeoutColor;


// ******************************************************************************************
#pragma mark -
#pragma mark Initialisation and object management

- (id)initWithFrame:(CGRect)frame
{
    if ((self = [super initWithFrame:frame])) {
        self.backgroundColor = [UIColor clearColor];
    }
    
    return self;
}

- (void)dealloc
{
    [strikeoutColor release];
    
    [super dealloc];
}


// ******************************************************************************************
#pragma mark -
#pragma mark Drawing support

void drawStrikeout(void *info, CGContextRef context)
{
    CGFloat size = [(NSNumber*)[(NSArray*)info objectAtIndex:0] floatValue];
    UIColor* colour = (UIColor*)[(NSArray*)info objectAtIndex:1];

//    CGContextSetStrokeColorWithColor(context, [UIColor darkGrayColor].CGColor);
//    CGContextBeginPath(context);
//    CGContextSetLineWidth(context, 1);
//    CGContextMoveToPoint(context, size + 1, 1);
//    CGContextAddLineToPoint(context, 1, size + 1);
//    CGContextStrokePath(context);

    //CGContextSaveGState(context);
    CGContextSetStrokeColorWithColor(context, colour.CGColor);
    CGContextBeginPath(context);
    CGContextSetLineWidth(context, 1);
    CGContextMoveToPoint(context, size, 0);
    CGContextAddLineToPoint(context, 0, size);
    CGContextStrokePath(context);
    //CGContextRestoreGState(context);
}

void releaseStrikeout(void *info)
{
    [(NSNumber*)info release];
}

- (void)drawRect:(CGRect)rect
{
    [super drawRect:rect];
    
    CGRect frameRect = self.bounds;
    
    CGContextRef context = UIGraphicsGetCurrentContext();
    CGContextSaveGState(context);
    CGContextSetStrokeColorWithColor(context, [UIColor whiteColor].CGColor);

    CGColorSpaceRef colorSpace = CGColorSpaceCreatePattern(NULL);
    CGContextSetFillColorSpace(context, colorSpace);
    CGColorSpaceRelease(colorSpace);
    
    CGFloat alpha = 0.85;
    static const CGPatternCallbacks callback = { 0, &drawStrikeout, &releaseStrikeout };
    
    CGFloat height = frameRect.size.height;
    NSArray* info = [[NSArray arrayWithObjects:
                      [NSNumber numberWithFloat:height],
                      strikeoutColor,
                      nil] retain];
    CGPatternRef pattern = CGPatternCreate(info,
                                           CGRectMake(0, 0, height, height),
                                           CGAffineTransformIdentity,
                                           height, height,
                                           kCGPatternTilingConstantSpacing,
                                           true,
                                           &callback);
    CGContextSetFillPattern(context, pattern, &alpha);
    CGPatternRelease(pattern);

    CGContextFillRect(context, frameRect);    

    CGContextRestoreGState(context);
}

@end
